#!/usr/bin/env python
#coding=utf-8

import db,utils,settings
from ex import NoPreDay

def update_change(con):
    c1 = con.cursor()
    
    log = open(settings.root + settings.log + '/ipo.log', 'w' )
    
    c1.execute('select date, code, close from price limit 100 ')
    for r in c1:
        ymd = [ int(i) for i in r[0].split('-') ]
        
        isostr = r[0]
        code = r[1]
        close = r[2]
        print code,isostr,close
        try:
            c2 = con.cursor()
            pre_close = db.pre_1day_close(c2, code, *ymd)
        except NoPreDay, e:
            log.write("%s, %s\n"%(e.code, e.date))
            log.flush()
            continue
        else:
            change = (close - pre_close)* 100 / pre_close
            print close,pre_close,change

con = utils.get_db_connector()
update_change(con)